home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / internet / web-related / apache_1.0.5 / cgi-src / change-passwd.c next >
Text File  |  1996-02-16  |  5KB  |  173 lines

  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/signal.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7.  
  8. #define USER_FILE "/usr/local/etc/httpd/conf/.htpasswd"
  9. #define WIZARD "surobm"
  10.  
  11. char *makeword(char *line, char stop);
  12. char *fmakeword(FILE *f, char stop, int *len);
  13. char x2c(char *what);
  14. void unescape_url(char *url);
  15. void plustospace(char *str);
  16.  
  17. char *crypt(char *pw, char *salt); /* why aren't these prototyped in include */
  18.  
  19.  
  20. char *tn;
  21.  
  22. /* From local_passwd.c (C) Regents of Univ. of California blah blah */
  23. static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
  24.         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  25.  
  26. to64(s, v, n)
  27.   register char *s;
  28.   register long v;
  29.   register int n;
  30. {
  31.     while (--n >= 0) {
  32.         *s++ = itoa64[v&0x3f];
  33.         v >>= 6;
  34.     }
  35. }
  36.  
  37. void change_password(char *user, char *pw, FILE *f) {
  38.     char *cpw, salt[3];
  39.  
  40.     (void)srand((int)time((time_t *)NULL));
  41.     to64(&salt[0],rand(),2);
  42.     cpw = crypt(pw,salt);
  43.     free(pw);
  44.     fprintf(f,"%s:%s\n",user,cpw);
  45. }
  46.  
  47. void putline(FILE *f,char *l) {
  48.     int x;
  49.  
  50.     for(x=0;l[x];x++) fputc(l[x],f);
  51.     fputc('\n',f);
  52. }
  53.  
  54. main(int argc, char *argv[]) {
  55.     register int x;
  56.     int cl,found,create;
  57.     char *u,*t1,*t2,*p1,*p2,*user, command[256], line[256], l[256], w[256];
  58.     FILE *tfp,*f;
  59.  
  60.     tn = NULL;
  61.  
  62.     printf("Content-type: text/html%c%c",10,10);
  63.  
  64.     if(strcmp(getenv("REQUEST_METHOD"),"POST")) {
  65.         printf("This script should be referenced with a METHOD of POST.\n");
  66.         printf("If you don't understand this, see this ");
  67.         printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html\">forms overview</A>.%c",10);
  68.         exit(1);
  69.     }
  70.     if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) {
  71.         printf("This script can only be used to decode form results. \n");
  72.         exit(1);
  73.     }
  74.     cl = atoi(getenv("CONTENT_LENGTH"));
  75.  
  76.     user=NULL;
  77.     p1=NULL;
  78.     p2=NULL;
  79.     create=0;
  80.     for(x=0;cl && (!feof(stdin));x++) {
  81.         t1 = fmakeword(stdin,'&',&cl);
  82.         t2 = makeword(t1,'=');
  83.         unescape_url(t1);
  84.         unescape_url(t2);
  85.         if(!strcmp(t2,"user")) {
  86.             if(!user)
  87.                 user = t1;
  88.             else {
  89.                 printf("This script was accessed from the wrong form.\n");
  90.                 exit(1);
  91.             }
  92.         }
  93.         else if(!strcmp(t2,"newpasswd1")) {
  94.             if(!p1)
  95.                 p1 = t1;
  96.             else {
  97.                 printf("This script was accessed from the wrong form.\n");
  98.                 exit(1);
  99.             }
  100.         }
  101.         else if(!strcmp(t2,"newpasswd2")) {
  102.             if(!p2)
  103.                 p2 = t1;
  104.             else {
  105.                 printf("This script was accessed from the wrong form.\n");
  106.                 exit(1);
  107.             }
  108.         }
  109.         else {
  110.             printf("This script was accessed from the wrong form.\n");
  111.             printf("Unrecognized directive %s.\n",t2);
  112.             exit(1);
  113.         }
  114.         free(t2);
  115.     }
  116.     u=getenv("REMOTE_USER");
  117.     if((strcmp(u,WIZARD)) && (strcmp(user,u))) {
  118.             printf("<TITLE>User Mismatch</TITLE>");
  119.             printf("<H1>User Mismatch</H1>");
  120.             printf("The username you gave does not correspond with the ");
  121.             printf("user you authenticated as.\n");
  122.             exit(1);
  123.         }
  124.     if(strcmp(p1,p2)) {
  125.         printf("<TITLE>Password Mismatch</TITLE>");
  126.         printf("<H1>Password Mismatch</H1>");
  127.         printf("The two copies of your the password do not match. Please");
  128.         printf(" try again.");
  129.         exit(1);
  130.     }
  131.  
  132.     tn = tmpnam(NULL);
  133.     if(!(tfp = fopen(tn,"w"))) {
  134.         fprintf(stderr,"Could not open temp file.\n");
  135.         exit(1);
  136.     }
  137.  
  138.     if(!(f = fopen(USER_FILE,"r"))) {
  139.         fprintf(stderr,
  140.                 "Could not open passwd file for reading.\n",USER_FILE);
  141.         exit(1);
  142.     }
  143.  
  144.     found = 0;
  145.     while(!(getline(line,256,f))) {
  146.         if(found || (line[0] == '#') || (!line[0])) {
  147.             putline(tfp,line);
  148.             continue;
  149.         }
  150.         strcpy(l,line);
  151.         getword(w,l,':');
  152.         if(strcmp(user,w)) {
  153.             putline(tfp,line);
  154.             continue;
  155.         }
  156.         else {
  157.             change_password(user,p1,tfp);
  158.             found=1;
  159.         }
  160.     }
  161.     if((!found) && (create))
  162.         change_password(user,p1,tfp);
  163.     fclose(f);
  164.     fclose(tfp);
  165.     sprintf(command,"cp %s %s",tn,USER_FILE);
  166.     system(command);
  167.     unlink(tn);
  168.     printf("<TITLE>Successful Change</TITLE>");
  169.     printf("<H1>Successful Change</H1>");
  170.     printf("Your password has been successfully changed.<P>");
  171.     exit(0);
  172. }
  173.